#!/bin/bash

# Slackware build script for wavelet-denoise
# Originally written by klaatu for separate+
# Adapted to wavelet-denoise by Stefano Guidoni

# 20220222 bkw: Modified by SlackBuilds.org, BUILD=2:
# - fix build on 15.0.
# - actually install files to $PKG/usr instead of the real /usr!
# - actually use SLKCFLAGS.
# - i486 => i586.
# - don't install useless INSTALL doc.
# - use a download URL that exists.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=wavelet-denoise
VERSION=${VERSION:-0.3.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    x86_64) ARCH=x86_64 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xzf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \+

SLKCFLAGS+=" -fcommon"
sed -i -e "s,-O3,$SLKCFLAGS," \
       -e '/^LIBS/s,$, -lm,' \
       src/Makefile

make

# 20220225 bkw: do not use 'make install'! It ignores DESTDIR and spams
# your /usr. However, it's safe to install the translations... but
# the stupid thing won't create the required directories by itself.
mkdir -p $PKG/usr/share/locale/{de,ru,it,et,pl}/LC_MESSAGES
make -C po install LOCALEDIR=$PKG/usr/share/locale

mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/gimp/2.0/plug-ins
install -s src/wavelet-denoise $PKG/usr/lib$LIBDIRSUFFIX/gimp/2.0/plug-ins

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  AUTHORS README ChangeLog COPYING THANKS TRANSLATIONS \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
